package org.mbds.wolf.seql;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mbds.wolf.seql.exceptions.MalformedConfigFileException;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

/**
 * 
 * @author alesas
 *
 */
public class SeqlMetadata 
{
	private File configFile = null;
	private SeqlApplets seqlApplets = null;
	private static SeqlMetadata instance = null;
	
	/**
	 * static getInstance
	 * @param configFile
	 * @param fileName
	 * @return SeqlMetadata singleton
	 */
	public static SeqlMetadata getInstance(File configFile) 
	{
		if (instance==null) 
		{
			instance = new SeqlMetadata(configFile);
		}
		return instance;
	}
	
	/**
	 * private constructor SeqlMetadata
	 * @param configFile
	 */
	private SeqlMetadata(File configFile) 
	{
		this.configFile = configFile;
	}

	/**
	 * getApplets
	 * @return AppletModel List
	 */
	public List<AppletModel> getApplets()
	{
		if (seqlApplets==null)
		{
			try 
			{
				seqlApplets = load();
	
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			} 
		}
		if(seqlApplets!=null) 
		{
			if(seqlApplets.getApplets()!=null)
			{
				return seqlApplets.getApplets();
			}
		} 
		return new ArrayList<AppletModel>();
	}

	/**
	 * load (config)
	 * @return SeqlApplets
	 * @throws Exception
	 */
	private SeqlApplets load() throws Exception 
	{
		Serializer serializer = new Persister();
		SeqlApplets seqlApplets = serializer.read(SeqlApplets.class, configFile);
		return seqlApplets;
	}

	/**
	 * getAppletByAlias
	 * @param appletAlias
	 * @return AppletModel
	 * @throws Exception
	 */
	public AppletModel getAppletByAlias(String appletAlias) throws Exception 
	{
		AppletModel applet = null;
		if (getApplets()!=null)
		{
			applet = seqlApplets.getAppletByAlias(appletAlias);
		}
		if (applet!=null)
		{
			return applet;
		}
		throw new MalformedConfigFileException();
	}
	
	/**
	 * getAppletByAID
	 * @param aID
	 * @return AppletModel
	 * @throws Exception
	 */
	public AppletModel getAppletByAID(String aID) throws Exception 
	{
		AppletModel applet = null;
		if (getApplets()!=null)
		{
			applet = seqlApplets.getAppletByAID(aID);
		}
		if (applet!=null)
		{
			return applet;
		}
		throw new MalformedConfigFileException();
	}

	/**
	 * remove
	 * @param applet
	 * @return boolean
	 * @throws Exception
	 */
	public boolean remove(AppletModel applet) throws Exception 
	{
//		System.out.println(applet + " should be removed after this");
		SeqlApplets seqlApplets = load();
		List<AppletModel> appletsModel = getApplets(); 
		for (AppletModel a : appletsModel) 
		{
			if (a.equals(applet)) 
			{
				appletsModel.remove(a);
				System.out.println(applet + " is removed");
			}
		}
		seqlApplets.setApplets(appletsModel);
		return persist(seqlApplets);
	}

	/**
	 * persist
	 * @param seqlApplets
	 * @return boolean
	 * @throws Exception
	 */
	private boolean persist(SeqlApplets seqlApplets) throws Exception 
	{
		Serializer serializer = new Persister();
		serializer.write(seqlApplets, configFile);
		return true;
	}

	/**
	 * save
	 * @param applet
	 * @return boolean
	 * @throws Exception
	 */
	public boolean save(AppletModel applet) throws Exception 
	{
		SeqlApplets seqlApplets;
		try
		{
			seqlApplets = load();
		}
		catch(Exception e)
		{
			seqlApplets= new SeqlApplets();
		}
		
		List<AppletModel> appletsModel = seqlApplets.getApplets();
		AppletModel exist = getAppletByAlias(applet.getAlias());
		
		if(exist!=null)
		{
			remove(applet);
		}
		
		if(appletsModel==null) 
		{
			appletsModel = new ArrayList<AppletModel>();
		}
		
		appletsModel.add(applet);
		return persist(seqlApplets);
	}

	/**
	 * getFileDirectory
	 * @return the fileDirectory
	 */
	public File getConfigFile() 
	{
		return configFile;
	}

}
